# from sklearn import preprocessing
# import numpy as np
# # from scipy.sparse import csr_matrix
# enc = preprocessing.OneHotEncoder()
# productData = np.array([[0, 0, 3], [1, 1, 0], [2, 2, 1], [1, 0, 2], [0, 1, 2]])
# enc.fit(productData)
# Ohe = enc.transform(productData)
# # print Ohe.__dict__
# # print Ohe.indptr
# # print Ohe.indices
# # print Ohe.data
# # print Ohe
# print (Ohe.toarray())   #结果参考P29页例子说明。
#
# #-----------------csr_matrix--------------------------#
# # indptr = np.array([0, 3, 6])
# # indices = np.array([0, 1, 2, 0, 1, 2])
# # data = np.array([1, 2, 3, 4, 5, 6])
# # print csr_matrix((data, indices, indptr)).toarray()
from sklearn import datasets
from sklearn.preprocessing import OneHotEncoder
import numpy as np

# 加载 Iris 数据集
iris = datasets.load_iris()
X = iris.data
y = iris.target.reshape(-1, 1)  # 转换为列向量，以便于OneHotEncoder处理

# 实例化 OneHotEncoder
enc = OneHotEncoder()

# 拟合并转换目标变量
y_onehot = enc.fit_transform(y)

# 打印转换后的数组
print(y_onehot.toarray())